package com.jamaskii.dictation;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.ClipData;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jamaskii.ecdict.Word;
import com.jamaskii.ecdict.WordBook;

import java.util.ArrayList;

public class WordsManageActivity extends BaseActivity {

    private ArrayList<ItemData> itemData=new ArrayList<>();
    private WordBook book;
    private ListView listManageWords;
    public static WordsManageActivity instance;
    ItemAdapter itemAdapter;
    private Handler hand=new Handler();

    public int getStatueBarHeight(){
        int statusBarHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(object).toString());
            statusBarHeight = getResources().getDimensionPixelSize(height);
            return statusBarHeight;
        } catch (Exception e) {
            return 24;
        }
    }

    private void setTopPadding(){
        ConstraintLayout layout=findViewById(R.id.padding) ;
        ViewGroup.LayoutParams layoutParams=layout.getLayoutParams();
        layoutParams.height=getStatueBarHeight();
        layout.setLayoutParams(layoutParams);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_words);
        instance=this;
        init();
    }

    private void init(){
        listManageWords=findViewById(R.id.list_manage_words);
        setOnBackClick();
        book=HomeActivity.instance.wordBookStream.wordBooks.get(BookActivity.instance.index);
        showWords();
        setTopPadding();
    }

    private void showWords(){
        itemData.clear();
        for (Word word:book.words
             ) {
            ItemData item=new ItemData(word,ItemType.MANAGE_WORD);
            itemData.add(item);
        }
        itemAdapter=new ItemAdapter(this,R.layout.item_book_manager,itemData);
        listManageWords.setAdapter(itemAdapter);
    }

    private void setOnBackClick(){
        ImageView imageBack=findViewById(R.id.image_back);
        imageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private ArrayList<ItemData> getSelectedItems(){
        ArrayList<ItemData> result=new ArrayList<>();
        for (ItemData item:itemData
        ) {
            if(item.checked){
                result.add(item);
            }
        }
        return result;
    }

    public void freshLabel(){
        int count=getSelectedItems().size();
        TextView labelAllSelect=findViewById(R.id.label_all_select);
        TextView labelDelete=findViewById(R.id.label_delete);
        if(count==0){
            labelDelete.setTextColor(getResources().getColor(R.color.defaultTextGary));
            labelDelete.setText("删除");
        }else{
            labelAllSelect.setText("全选");
            labelDelete.setText("删除("+count+")");
            labelDelete.setTextColor(getResources().getColor(R.color.themeColor));
        }
        if(count==itemData.size()){
            labelAllSelect.setText("全不选");
        }
    }

    public void onLabelClick(View view){
        switch (view.getId()){
            case R.id.label_all_select:
                boolean selectAll=getSelectedItems().size()!=itemData.size();
                for (CheckBox checkBox:itemAdapter.checkBoxes
                ) {
                    checkBox.setChecked(selectAll);
                }
                break;
            case R.id.label_delete:
                final ArrayList<ItemData> items=getSelectedItems();
                String msg="确认删除这 "+items.size()+" 个单词吗？";
                if(items.size()==1){
                    msg="确认删除单词 "+items.get(0).wordToBeShow.name+" ？";
                }
                AlertDialog.Builder builder=new AlertDialog.Builder(this);
                builder.setNegativeButton("否",null);
                builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        for (ItemData item:items
                        ) {
                            book.words.remove(item.wordToBeShow);
                        }
                        book.save(HomeActivity.instance.wordBookStream.path);
                        showWords();
                        BookActivity.instance.showWords();
                        freshLabel();
                    }
                });
                builder.setTitle("删除");
                builder.setMessage(msg);
                AlertDialog dialog=builder.create();
                dialog.show();
                dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.themeColor));
                dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.themeColor));
                break;
        }
    }

}